package com.mobartisan.workinglunch.util;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

import java.io.File;

/**
 * Created by Administrator on 2016/3/17 0017.
 */
public class CameraUtils {
    public static final String IMAGE_TYPE = "image/*";

    /**
     * 打开照相机
     *
     * @param activity    当前的activity
     * @param requestCode 拍照成功时activity forResult 的时候的requestCode
     * @param photoFile   拍照完毕时,图片保存的位置
     */
    public static void launchCamera(Activity activity, int requestCode,
                                    File photoFile) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
        activity.startActivityForResult(intent, requestCode);
    }

    public static void launchCamera(Activity activity, int requestCode) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra("return-data", true); // 设置为true 的时候才能有返回
        //intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
        activity.startActivityForResult(intent, requestCode);
    }

    /**
     * 本地照片调用
     *
     * @param activity
     * @param requestCode
     */
    public static void launchGallery(Activity activity, int requestCode) {
        if (launchSys(activity, requestCode)
                && launch3partyBroswer(activity, requestCode)
                && launchFinally(activity)) ;
    }

    /**
     * PopupMenu打开本地相册.
     */
    private static boolean launchSys(Activity activity, int actResultCode) {
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                IMAGE_TYPE);
        try {
            activity.startActivityForResult(intent, actResultCode);

        } catch (android.content.ActivityNotFoundException e) {

            return true;
        }

        return false;
    }

    /**
     * 打开其他的一文件浏览器,如果没有本地相册的话
     */
    private static boolean launch3partyBroswer(Activity activity, int requestCode) {
        Toast.makeText(activity, "没有相册软件，运行文件浏览器", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT); // "android.intent.action.GET_CONTENT"
        intent.setType(IMAGE_TYPE); // 查看类型 String IMAGE_UNSPECIFIED =
        // "image/*";
        Intent wrapperIntent = Intent.createChooser(intent, null);
        try {
            activity.startActivityForResult(wrapperIntent, requestCode);
        } catch (android.content.ActivityNotFoundException e1) {
            return true;
        }
        return false;
    }

    /**
     * 这个是找不到相关的图片浏览器,或者相册
     */
    private static boolean launchFinally(Activity activity) {
        Toast.makeText(activity, "您的系统没有文件浏览器或则相册支持,请安装！", Toast.LENGTH_LONG)
                .show();
        return false;
    }

    /**
     * 获取从本地图库返回来的时候的URI解析出来的文件路径
     *
     * @return
     */
    public static String getPhotoPathByLocalUri(Context context, Intent data) {
//        Uri selectedImage = data.getData();
        Uri selectedImage = geturi(context, data);

        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = context.getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        return picturePath;
    }


    public static Uri geturi(Context context, Intent intent) {
        Uri uri = intent.getData();
        String type = intent.getType();
        if (uri.getScheme().equals("file") && (type.contains("image/"))) {
            String path = uri.getEncodedPath();
            if (path != null) {
                path = Uri.decode(path);
                ContentResolver cr = context.getContentResolver();
                StringBuffer buff = new StringBuffer();
                buff.append("(").append(MediaStore.Images.ImageColumns.DATA).append("=")
                        .append("'" + path + "'").append(")");
                Cursor cur = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        new String[]{MediaStore.Images.ImageColumns._ID},
                        buff.toString(), null, null);
                int index = 0;
                for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
                    index = cur.getColumnIndex(MediaStore.Images.ImageColumns._ID);
                    // set _id value
                    index = cur.getInt(index);
                }
                if (index == 0) {
                    // do nothing
                } else {
                    Uri uri_temp = Uri
                            .parse("content://media/external/images/media/"
                                    + index);
                    if (uri_temp != null) {
                        uri = uri_temp;
                        Log.i("urishi", uri.toString());
                    }
                }
            }
        }
        return uri;
    }

    /**
     * 剪切图片 在 onActivityResult的时候 从 返回的 Intent 里面获取 data 就是bitmap >>>> Bitmap
     * bitmap = data.getParcelableExtra("data");
     *
     * @param activity
     * @param imagePath   文件路径
     * @param requestCode 返回码
     */
    public static void startHeadCrop(Activity activity, String imagePath,
                                     int requestCode, boolean isLarge) {
        Log.d("MenuFragment", "startHeadCrop: ");
        String headpath = FileUtil.getHeadFilePath(activity);
        File f = new File(imagePath);
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(Uri.fromFile(f), "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 4); // 裁剪框比例 4 : 3
        intent.putExtra("aspectY", 3);
        intent.putExtra("outputX", isLarge ? 400 : 200);
        intent.putExtra("outputY", isLarge ? 300 : 150);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", true); // 设置为true 的时候才能有返回
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(headpath)));
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true); // no face detection
        activity.startActivityForResult(intent, requestCode);
    }

    /**
     * 剪切图片 在 onActivityResult的时候 从 返回的 Intent 里面获取 data 就是bitmap >>>> Bitmap
     * bitmap = data.getParcelableExtra("data");
     *
     * @param activity
     * @param imagePath   文件路径
     * @param requestCode 返回码
     */
    public static void startImgCrop(Activity activity, String imagePath,
                                    int requestCode, boolean isLarge) {
        //String  imgPath=FileUtil.getCacheImgPath(activity);
        File f = new File(imagePath);
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(Uri.fromFile(f), "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 4); // 裁剪框比例 4 : 3
        intent.putExtra("aspectY", 3);
        intent.putExtra("outputX", isLarge ? 400 : 200);
        intent.putExtra("outputY", isLarge ? 300 : 150);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", true); // 设置为true 的时候才能有返回
        //intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(imgPath)));
        //intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true); // no face detection
        activity.startActivityForResult(intent, requestCode);
    }
}
